<style lang="scss">
	@import "../../utils/base.scss";
</style>
<template>
	<div id='safeCheck' class="quest_content" style="overflow-y:auto;height:100%;width:100%;">
		<!-- 租户详情 -->
		<template id="safeCheck">
			<div class="qheader" :style="{borderBottom: ifedit ? '0px solid rgb(210,210,210)' : '1px solid rgb(210,210,210)',}">
				<p>申请信息</p>
			</div>
			<div class="gcontent">
				<Form ref="safeCheck" :model="applyinfo.safeCheck" :rules="projectInfoValidate" :label-width="labelWidth"
				 label-position="left" class="form form_left">
					<Row v-if="reqId != null && reqId != ''">
						<Col span="8">
						<FormItem label="工单ID：">
							<Input v-model="reqId" :readonly="readonly" />
						</FormItem>
						</Col>
					</Row>
					<Row>
						<Col span="8" >
						<FormItem label="申请人:">
							<Input  v-model="userInfo.user_name" :readonly="readonly"  />
						</FormItem>
						</Col>
						<Col span="8" class="col_left">
						<FormItem label="申请公司:"  >
							<Input  v-model="userInfo.department" :readonly="readonly"  />
						</FormItem>
						</Col>
					</Row>
					<Row >
						<Col span="8" >
						<FormItem label="申请人邮箱:"  >
							<Input  v-model="userInfo.email" :readonly="readonly"  />
						</FormItem>
						</Col>
						<Col span="8"  class="col_left" >
						<FormItem label="申请人手机:" >
							<Input  v-model="userInfo.phone" :readonly="readonly"  />
						</FormItem>
						</Col>
					</Row>
					<Row>
						<Col span="8">
						<FormItem label="工单标题：" prop="reqSubject">
							<Input v-model="applyinfo.safeCheck.reqSubject" placeholder="XXX资源池+XXX系统+标题描述" :readonly="ifedit" />
						</FormItem>
						</Col>
					</Row>
					<Row>
						<Col span="8">
						<FormItem label="业务负责人姓名：" prop="charge">
							<Input v-if="ifedit" v-model="applyinfo.safeCheck.charge" :readonly="readonly" />
							<component v-else ref="inputSearch"  :dataList="userList" :checkLable="applyinfo.safeCheck.chId" :is="'inputSearch'"
							 :isSysUser="false" :multiple="false" @on-change="checkIt"></component>
							<!-- <Input v-model="applyinfo.safeCheck.purpose" placeholder="部门+姓名" :readonly="ifedit" /> -->
						</FormItem>
						</Col>
						<Col span="8" class="col_left">
						<FormItem label="业务负责人部门：" prop="chBranch">
							<Input v-if="ifedit" v-model="applyinfo.safeCheck.chBranch" :readonly="true" />
							<Select v-else v-model="applyinfo.safeCheck.chBranch">
								<Option v-for="(item,index) in deptList" :key="index" :value="item.gName">{{item.gName}}</Option>
							</Select>
						</FormItem>
						</Col>
					</Row>
					<Row>
						<Col span="8">
						<FormItem label="工单发起时间：" prop="startTime">
							<DatePicker :options="disabledDate" v-model="applyinfo.safeCheck.startTime" style="width: 100%;" type="date"
							 :disabled="ifedit" placeholder="请选择日期"></DatePicker>
						</FormItem>
						</Col>
						<Col span="8" class="col_left">
						<FormItem label="期望完成时间：" prop="hopeTime">
							<!-- <Input  :readonly="readonly" /> -->
							<DatePicker :options="disabledDate" v-model="applyinfo.safeCheck.hopeTime" style="width: 100%;" type="date"
							 :disabled="ifedit" placeholder="请选择日期"></DatePicker>
						</FormItem>
						</Col>
					</Row>
					<Row>
						<Col span="22" style="width:96.666667%">
						<FormItem label="申请原因：" prop="purpose">
							<Input v-model='applyinfo.safeCheck.purpose' type="textarea" :autosize="{minRows: 4,maxRows: 6}" :readonly="ifedit" />
						</FormItem>
						</Col>
					</Row>
				</Form>
			</div>

		</template>

		<!-- 附件 -->
		<template>
			<div class="qheader" :style="{borderBottom: ishidden ? '0px solid rgb(210,210,210)' : '1px solid rgb(210,210,210)',}">
				<p>附件</p>
			</div>
			<component ref="file" :is="'file'" :ifedit="ifedit" :filesList="filesList" v-model="applyinfo.files"></component>
		</template>

	</div>
</template>
<script>
	import ajax from "../../ajax_util"
	import Axios from 'axios'
	import notice from '../../base/notice'
	import file from './settledin/file'
	import inputSearch from '../../base/input_search'

	export default {
		name: "apply",
		components: {
			notice,
			file,
			inputSearch
		},
		data() {
			const validateEntryEndTime = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请选择期望完成时间'));
				} else {
					if (this.applyinfo.safeCheck.startTime === '') {
						callback(new Error('请选择工单发起时间'));
					} else if (this.applyinfo.safeCheck.startTime.getTime() > value.getTime()) {
						callback(new Error('期望完成时间不能小于工单发起时间'));
					} else {
						callback()
					}
				}
			};
			const validatecharge = (rule, value, callback) => {
				console.log("value---->", value)
				if (this.applyinfo.safeCheck.charge == '') {
					callback(new Error('请选择业务负责人'));
				} else {
					callback()
				}
			};

			return {
				deptList: [],
				mdvalue: "",
				announcement: true,
				showmd: true,
				serviceType: "securityCheck",
				dataInfoEdit: 0,
				showFileMould: false,
				httpHeader: {
					Authorization: this.$store.state.token
				},
				// ifedit: this.isedit,
				disabledDate: {
					disabledDate(date) {
						return date && date.valueOf() < Date.now() - 24 * 60 * 60 * 1000 || date.valueOf() > Date.now() + 365 * 24 * 60 *
							60 * 1000;
					}
				},
				projectInfoValidate: {
					startTime: [{
						required: true,
						type: 'date',
						message: '请选择工单发起时间',
						trigger: 'change'
					}],
					hopeTime: [{
						required: true,
						validator: validateEntryEndTime,
						trigger: 'blur'
					}],
					charge: [{
						required: true,
						validator: validatecharge,
						trigger: 'change'
					}],
					purpose: [{
						required: true,
						type: 'string',
						message: '请填写申请原因',
						trigger: 'change'
					}],
					reqSubject: [{
							required: true,
							type: 'string',
							message: '请填写需求主题',
							trigger: 'blur'
						},
						{
							type: 'string',
							max: 50,
							message: '最多填写50个字符',
							trigger: 'blur'
						}
					],

				},

				fileUrl: this.CONFIG.file_url,
				readonly: true, //是否只读
				labelWidth: 140,
				bealoonTrue: true,
				file: null,
				loadingStatus: false,
				fileUpLoadAction: "",
				name: "",
				userInfo: {},
				userList: [],
				demandAssign: "",
				applyinfo: {
					safeCheck: {
						chId: "",
						charge: "",
						purpose: "",
						startTime: "",
						hopeTime: "",
						reqSubject: "",
						userId: "",
						userName: "",
						applyCardId: "",
						telephone: "",
						email: "",
						applydept: ""

					},
					datas: [],
					files: []
				}
			}
		},
		created: function() {},
		mounted: function() {
			console.log("this.reqId,this.draftsId---------->", this.reqId, this.draftsId)
			console.log('this.$store.state.tabs', this.$store.state.tabs)
			this.$nextTick(() => {
				if (this.reqId == null && this.draftsId == null) {
					this.applyinfo.safeCheck.startTime = new Date()
					this.getUserInfo();
				} else if (this.reqId != null) {
					console.log("获取表单详情---------->");
					this.getApplyInfo();
				} else if (this.draftsId != null) {
					console.log("获取草稿---------->")
					this.getDrafts();
				}
			})

		},

		methods: {
			checkIt(list) {
				console.log('checkIt', list)
				list.forEach(item => {
					this.applyinfo.safeCheck.charge = item.user_name
					this.applyinfo.safeCheck.chId = item.id
					this.demandAssign = item.id
					this.deptList = item.structs
					if (item.structs.length > 0) {
						this.applyinfo.safeCheck.chBranch = this.deptList[0].gName
					}
				})
				console.log(this.applyinfo.safeCheck.chId)
				this.userList = list
			},

			formatDate(value) {
				var date = new Date(value * 1000);
				var year = date.getFullYear();
				var month = this.padDate(date.getMonth() + 1);
				var day = this.padDate(date.getDate());
				return year + '-' + month + '-' + day;
			},
			padDate(value) {
				return value < 10 ? '0' + value : value;
			},

			validate() {
				var isSubmit = false
				this.$refs["safeCheck"].validate((valid) => {
					if (valid) {
						//var check = true
						if (this.applyinfo.safeCheck.charge == '') {
							this.$Message.error('请填写业务负责人姓名');
							isSubmit = false
						} else {
							isSubmit = true
						}
					} else {
						this.$Message.error('请填写必填项');
					}
				})
				return isSubmit
			},

			//获取租户角色
			// 获取提交表单人基本信息
			getApplyUser(userId) {
				var that = this;
				var url = this.CONFIG.url_know + '/user/' + userId;
				ajax.get({
					url: url,
					that: that,
					success: function(data) {
						console.log("user----->", data)
						that.userInfo = data
						that.userInfo.department = that.processInfo.dptName
					}
				})
			},

			getUserInfo() {
				this.userInfo = this.$store.state.user
				console.log("获取用户基础信息------->", this.$store.state.user)
				this.applyinfo.safeCheck.userId = this.$store.state.user.id

			},
			//获取草稿详情
			getDrafts() {
				var url = this.CONFIG.base_url_test + "/draft/content/" + this.draftsId
				var that = this;
				ajax.get({
					url: url,
					that: that,
					params: null,
					success: function(data) {
						that.handleInfo(data.req)
						that.getUserInfo()
					}
				})

			},
			//转换info格式
			handleInfo(data) {
				console.log("handle ---->", data)
				var info = this.applyinfo

				info.files = data.files
				info.datas = data.datas
				info.safeCheck = data.object
				info.safeCheck.startTime = this.formatDate(data.object.startTime)
				info.safeCheck.hopeTime = this.formatDate(data.object.hopeTime)
				this.applyinfo.safeCheck.chBranch = data.object.chBranch
				this.userList = [{
					id: data.object.chId,
					user_name: data.object.charge
				}]
				if (this.applyinfo.safeCheck.chBranch != undefined && this.applyinfo.safeCheck.chBranch != "") {
					this.deptList = [{
						gName: this.applyinfo.safeCheck.chBranch
					}]
				}
				this.demandAssign = data.base.demandAssign.chId
			},

			getApplyInfo() {
				var data = this.processInfo
				this.getApplyUser(data.content.userId);

				this.applyinfo.files = data.files
				this.applyinfo.safeCheck = data.content
				if(this.ifedit){
					this.applyinfo.safeCheck.startTime = this.formatDate(data.content.startTime)
					this.applyinfo.safeCheck.hopeTime = this.formatDate(data.content.hopeTime)
				}
				this.userList = [{
					id: this.applyinfo.safeCheck.chId,
					user_name: this.applyinfo.safeCheck.charge
				}]
				this.deptList = [{
					gName: this.applyinfo.safeCheck.chBranch
				}]
				this.demandAssign = this.applyinfo.safeCheck.chId
			},

			//获取表单信息并加以处理
			getInfo() {
				this.demandAssign = this.applyinfo.safeCheck.chId
				if (this.applyinfo.safeCheck.chBranch == undefined || this.applyinfo.safeCheck.chBranch == null) {
					this.applyinfo.safeCheck.chBranch = ""
				}
				var t = JSON.stringify(this.applyinfo)
				var temp = JSON.parse(t)

				var info = {
					base: {
						dptName: this.userInfo.department,
						demandAssign: this.demandAssign
					},
					object: {},
				}
				info.object = temp.safeCheck
				info.object.startTime = new Date(temp.safeCheck.startTime).getTime() / 1000
				info.object.hopeTime = new Date(temp.safeCheck.hopeTime).getTime() / 1000

				info.files = this.applyinfo.files
				//处理resourceList
				//info.severs = temp.severs
				var publicInfo = this.utils.setPublicInfoInfo(info.object.reqSubject, info.object.startTime, info.object.hopeTime,
					info.object.purpose)
				info.puslicInfo = publicInfo
				info.demandAssign = this.demandAssign
				console.log("----info----", info);
				return info;
			},
			//格式化日期
			formatDate(value) {
				var date = new Date(value * 1000);
				var year = date.getFullYear();
				var month = this.padDate(date.getMonth() + 1);
				var day = this.padDate(date.getDate());
				return year + '-' + month + '-' + day;
			},
			padDate(value) {
				return value < 10 ? '0' + value : value;
			},
			// 获取处理时需要提交的参数
			getHandleParams(handleInfo) {
				handleInfo.reqSubject = this.applyinfo.safeCheck.reqSubject
				handleInfo.startTime = new Date(this.applyinfo.safeCheck.startTime).getTime() / 1000
				handleInfo.hopeTime = new Date(this.applyinfo.safeCheck.hopeTime).getTime() / 1000
				handleInfo.reqDes = this.applyinfo.safeCheck.purpose
				handleInfo.isEdit = this.isEdit;
				console.log("aaaa-------->", this.ifedit, this.isEdit == 1)
				handleInfo.isChangeContent = 0;
				if (!this.ifedit) {
					handleInfo.isChangeContent = 1;
				}
				if (this.isEdit == 1) {
					handleInfo.isChangeContent = 1;
					handleInfo.content = this.getInfo()
				}
			},
			//处理页面获取下一步处理人需要的参数
			getHandleUserParams(opOptions) {
				var param = {}
				param.vari = opOptions
				param.managerId = this.applyinfo.safeCheck.chId
				return param
			},

		},
		props: {
			isEdit: {},
			ifedit: {
				type: Boolean,
				default: false,
			},
			reqId: {
				default: null
			},
			draftsId: {
				default: null
			},
			filesList: {},
			processInfo: {},
			ishidden: {
				type: Boolean,
				default: false,
			},
		},
		// computed:{
		//     isEdit(){
		//         return this.$store.state.tabs != undefined && this.$store.state.tabs!=null? this.$store.state.tabs.isEdit:""
		//     }
		// },
		// watch:{
		//     isEdit(Val){
		//         if((Val + '') === (this.reqId + '')){
		//             this.ifedit = false
		//         }else{
		//             this.ifedit = true
		//         }
		//     }
		// }

	}
</script>
